﻿using System;
using System.ComponentModel;

namespace Needle.Utils.Extensions
{
    /// <summary>
    /// EventHandler extensions
    /// </summary>
    public static class EventHandlerExtensions
    {

        /// <summary></summary>
        public static void RaiseIfNotNull(this EventHandler eventHandler, object sender, EventArgs e)
        {
            if (eventHandler != null)
                eventHandler(sender, e);
        }

        /// <summary></summary>
        public static bool RaiseIfNotNull(this CancelEventHandler eventHandler, object sender, CancelEventArgs e)
        {
            if (eventHandler != null)
                eventHandler(sender, e);
            return e == null ? false : e.Cancel;
        }

        /// <summary></summary>
        public static void RaiseIfNotNull(this  PropertyChangedEventHandler eventHandler, object sender, string propertyName)
        {
            if (eventHandler != null)
                eventHandler(sender, new PropertyChangedEventArgs(propertyName));
        }

        /// <summary></summary>
        public static void RaiseIfNotNull(this  PropertyChangingEventHandler eventHandler, object sender, string propertyName)
        {
            if (eventHandler != null)
                eventHandler(sender, new PropertyChangingEventArgs(propertyName));
        }

        /// <summary></summary>
        public static void RaiseIfNotNull<TEventArgs>(this EventHandler<TEventArgs> eventHandler, object sender, TEventArgs e)
            where TEventArgs : EventArgs
        {
            if (eventHandler != null)
                eventHandler(sender, e);
        }

    }
}
